package com.jun.seckill_by_jun.controller;

import com.jun.seckill_by_jun.domain.Test;
import com.jun.seckill_by_jun.rabbitmq.MQReceiver;
import com.jun.seckill_by_jun.rabbitmq.MQSender;
import com.jun.seckill_by_jun.redis.KeyPrefix;
import com.jun.seckill_by_jun.redis.RedisService;
import com.jun.seckill_by_jun.redis.UserKey;
import com.jun.seckill_by_jun.result.CodeMsg;
import com.jun.seckill_by_jun.result.Result;
import com.jun.seckill_by_jun.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class SampleController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private TestService testService;

    @Autowired
    private MQReceiver mqReceiver;

    @Autowired
    private MQSender mqSender;


    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("name","jun");
        return "hello";
    }

    @ResponseBody
    @RequestMapping("/testMybatis")
    public String testMybatis(Model model){
        Test test = new Test(1,"jun");
        testService.insert(test);
        Test ans = testService.getTestById(1);
        return ans.toString();
    }

    @ResponseBody
    @RequestMapping("/testTransaction")
    public String testTransaction(){
        boolean b = testService.transactionTest();
        if(b){
            return "success";
        }else{
            return "false";
        }
    }

    @ResponseBody
    @RequestMapping("/testResultSuccess")
    public Result<String> testResultSuccess(){
        return Result.success("go on!");
    }

    @ResponseBody
    @RequestMapping("/testResultError")
    public Result testResultError(){
        return Result.error(CodeMsg.SERVER_ERROR);
    }

    @ResponseBody
    @RequestMapping("/redis/get")
    public Result<Long> redisGet(){
        Long v1 =  redisService.get(UserKey.getById,"1",Long.class);
        return Result.success(v1);
    }

    @ResponseBody
    @RequestMapping("/redis/set")
    public Result<Long> redisSet(){
        redisService.set(UserKey.getById,"1",123456);
        return Result.success(redisService.get(UserKey.getById,"1",Long.class));
    }

//    @ResponseBody
//    @RequestMapping("/mq")
//    public Result<String> mq(){
//        mqSender.send("hello!");
//        return Result.success("hello!");
//    }
//    @ResponseBody
//    @RequestMapping("/mq/topic")
//    public Result<String> mqtopic(){
//        mqSender.sendTopic("hello!");
//        return Result.success("hello!");
//    }

}
